<?php

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
/**
 * Description of Balance
 *
 * @author hoangle
 */
class Balance {

//put your code here
    private $db;
    
    public function  __construct() {
        $this->db = Zend_Registry::get('db');
    }

    /**
     * @desc Insert account info
     * @param <array> $arrParams
     * @return true | false
     */
    public function insertBalance($arrParams) {
        try {
            $sql = "INSERT INTO tblbalances (userid, totalcredit) VALUES(:userid, :credit)";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':userid', $arrParams['userid'], PDO::PARAM_INT);
            $stmt->bindParam(':credit', $arrParams['credit'], PDO::PARAM_INT);
            $stmt->execute();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return true;
    }

    /**
     * @desc Get data info user
     * @param <array> $arrParams
     * @return <array>
     */
    public function getBalanceByUser($arrParams) {
        try {
            $stmt = $this->db->prepare('CALL sp_getBalanceUser(:p_userid);');
            $stmt->bindParam(':p_userid', $arrParams['userid'], PDO::PARAM_STR);
            $stmt->execute();
            $rows = $stmt->fetch();
            $stmt->closeCursor();
            $this->db->closeConnection();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return $rows;
    }


    /**
     * @desc Decrease balance of user
     * @param <type> $arrData
     * @return <type>
     */
    public function creaseCreditByUserId($userid) {
        try {
            $sql = "UPDATE tblbalances SET totalcredit=totalcredit + 1 WHERE userid=:p_userid";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':p_userid', $userid, PDO::PARAM_INT);
            $stmt->execute();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e);
        }
        return true;
    }

}

?>
